package com.zhu.service.cms.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhu.service.cms.pojo.CmsAdType;
import com.zhu.service.cms.service.ICmsAdTypeService;
import com.zhu.commons.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 推荐位 前端控制器
 * </p>
 *
 * @author zhu
 * @since 2022-08-14
 */
@RestController
@RequestMapping("admin/cms/ad/type")
@Api(tags = "广告类别模块")
public class CmsAdTypeController {
    @Autowired
    private ICmsAdTypeService adTypeService;

    @PostMapping("save")
    @ApiOperation(value = "新增推荐分类")
    public R save(@RequestBody CmsAdType cmsAdType){
        boolean save = adTypeService.save(cmsAdType);
        if(save){
            return R.ok().message("添加成功！");
        }
        return R.error().message("添加失败！");
    }

    @GetMapping("get/{id}")
    @ApiOperation(value = "根据id查询广告分类")
    public R getadTypeById(@PathVariable String id){
        CmsAdType cmsAdType = adTypeService.getById(id);
        if(cmsAdType!=null){
            return R.ok().data("item",cmsAdType);
        }
        return R.error().message("error！");
    }

    @PutMapping("update")
    @ApiOperation(value = "更新推荐分类")
    public R updateadType(@RequestBody CmsAdType cmsAdType){
        boolean b = adTypeService.updateById(cmsAdType);
        if(b){
            return R.ok().message("更新成功！");
        }
        return R.error().message("更新失败！");
    }

    @DeleteMapping("delete/{id}")
    @ApiOperation(value = "删除推荐分类")
    public R removeById(@PathVariable String id)  {
        boolean b = adTypeService.removeById(id);
        if(b){
            return R.ok().message("删除成功！");
        }
        return R.error().message("删除失败！");
    }

    @ApiOperation("推荐类别分页列表")
    @GetMapping("list/{page}/{limit}")
    public R listPage(@PathVariable Long page,@PathVariable Long limit){
        Page<CmsAdType> pages=new Page<>(page,limit);
        IPage<CmsAdType> iPage= adTypeService.page(pages);
        List<CmsAdType> records = iPage.getRecords();
        long total = iPage.getTotal();

        if(records!=null){
            return R.ok().data("items",records).data("total",total);
        }
        return  R.error().message("error！");


    }

    @ApiOperation("查询所有推荐类别")
    @GetMapping("list")
    public R list(){
        List<CmsAdType> list = adTypeService.list();
        if(list!=null){
            return R.ok().data("items",list);
        }
        return  R.error().message("error！");


    }

}
